/* $RCSFile: LangGridView.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/LangGridView.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Globalization;
using System.Web;
using System.Security;
using System.Security.Permissions;
using System.Web.UI.WebControls;
using AddonNice.Helpers;

namespace AddonNice.UI.WebControls
{
	/// <summary>
	/// DataGrid to edit languages and cultures, used by LangListDataType
	/// </summary>
	public class LangGridView : GridView 
	{
        public LangGridView()
		{
			AutoGenerateColumns         =   false;

            TemplateField colUILang     =   new TemplateField();
			colUILang.HeaderText        =   LocalizeHelper.Localize("LANG_SWITCH_UI_LANGUAGE","UI Language");
            colUILang.ItemTemplate      =   new DropDownListLangTemplate(CultureTypes.AllCultures);
			Columns.Add(colUILang);

            TemplateField colCulture    =   new TemplateField();
			colCulture.HeaderText       =   LocalizeHelper.Localize("LANG_SWITCH_CULTURE","Culture");
            colCulture.ItemTemplate     =   new DropDownListLangTemplate(CultureTypes.SpecificCultures);
			Columns.Add(colCulture);

            CommandField colDelete      =   new CommandField();
			colDelete.ButtonType        =   ButtonType.Button;  
            colDelete.DeleteText        =   LocalizeHelper.Localize("LANG_SWITCH_DELETE", "Delete");  
			colDelete.ShowDeleteButton  =   true;
			Columns.Add(colDelete);

            CommandField colAdd         =   new CommandField();
            colAdd.ButtonType           =   ButtonType.Button;
            colAdd.ShowInsertButton     =   true;
            colAdd.InsertText           =   LocalizeHelper.Localize("LANG_SWITCH_ADD", "Add");  
			Columns.Add(colAdd);
		}


		/// <summary>
		/// Updates data sources with grid content
		/// </summary>
		public void UpdateRows()
		{
			LanguageCultureCollection LanguagesDataSource = (LanguageCultureCollection) DataSource;

			for(Int32 i = 0; i < this.Rows.Count; i++)
			{
				DropDownList ddLanguage             =   (DropDownList) Rows[i].Cells[0].Controls[0];
				DropDownList ddCulture              =   (DropDownList) Rows[i].Cells[1].Controls[0];

				LanguagesDataSource[i].UICulture    =   new CultureInfo(ddLanguage.SelectedItem.Value);
				LanguagesDataSource[i].Culture      =   new CultureInfo(ddCulture.SelectedItem.Value);
			}
		}

		/// <summary>
		/// Binds data source to grid
		/// </summary>
		protected override void DataBind(bool Raise)
		{
			base.DataBind(false);

			LanguageCultureCollection LanguagesDataSource = (LanguageCultureCollection) DataSource;

			if (LanguagesDataSource.Count > 1)
				Columns[2].Visible      =   true;
			else
				Columns[2].Visible      =   false; //do not allow deleting last row

			for(Int32 i = 0; i < Rows.Count; i++)
			{
                DropDownList ddLanguage =   (DropDownList)Rows[i].Cells[0].Controls[0];
                DropDownList ddCulture  =   (DropDownList)Rows[i].Cells[1].Controls[0];

				if (ddLanguage.Items.FindByValue(LanguagesDataSource[i].UICulture.Name) != null)
					ddLanguage.Items.FindByValue(LanguagesDataSource[i].UICulture.Name).Selected = true;

				if (ddCulture.Items.FindByValue(LanguagesDataSource[i].Culture.Name) != null)
					ddCulture.Items.FindByValue(LanguagesDataSource[i].Culture.Name).Selected = true;
			}
		}


        protected override void OnRowCreated(GridViewRowEventArgs e)
        {
            // The GridViewCommandEventArgs class does not contain a 
            // property that indicates which row's command button was
            // clicked. To identify which row's button was clicked, use 
            // the button's CommandArgument property by setting it to the 
            // row's index.
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // Retrieve the LinkButton control from the first column.
                LinkButton addButton = (LinkButton)e.Row.Cells[0].Controls[0];
                // Set the LinkButton's CommandArgument property with the row's index.
                addButton.CommandArgument = e.Row.RowIndex.ToString();
            }
            base.OnRowCreated(e);
        }

        /// <summary>
		/// Occours when user clicks on ADD or DELETE
		/// </summary>
        protected override void OnRowCommand(GridViewCommandEventArgs e)
		{
			LanguageCultureCollection LanguagesDataSource = (LanguageCultureCollection) DataSource;

			switch(e.CommandName)
			{
				case "Add":
					LanguagesDataSource.Insert(int.Parse(e.CommandArgument.ToString()), new LanguageCultureItem());
					DataBind();
					break;
				case "Delete":
                    LanguagesDataSource.RemoveAt(int.Parse(e.CommandArgument.ToString()));
					DataBind();
					break;
				case "Up":
					break;
				case "Down":
					break;
			}
			base.OnRowCommand(e);
		}
	}
}
